<?php

namespace app\index\controller;

use think\Controller;
// use app\index\model\User;
use think\Db;
use app\index\controller\Basecontroller;

class Commuity extends Basecontroller
{ 

  function __construct()
  {
    parent::__construct();

  }

  function getmsg()
  {
    $StoryArr = Db::table('gjc_story')
      ->alias('s')
      ->join('user u', 'u.uid = s.s_sponsor')
      ->order('s_created_time','desc')
      ->select();
    $ConmentArr = Db::table('gjc_comment_commuity')
      ->alias('sc')
      ->join('user u', 'u.uid = sc.uid')
      ->select();
    $result = array();
    $result = array('Story' => $StoryArr, 'Conmen' => $ConmentArr);

    return res(1000, '发送成功', $result);
    // var_dump($result);
  }

  function InDy()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $nowid = $nowSendM['nowid'];
    $message = $nowSendM['Fontcomment'];
    $img = $nowSendM['imgurl'];
    $title = $nowSendM['title'];
    if (empty($img)) {
      $data = [
        's_sponsor' => $nowid,
        's_main' => $message,
        's_created_time' => date('Y-m-d H:i:s', time()),
        's_title' => $title,
        's_img' => null,
        'like_num' => '0'
      ];
    } else {
      $data = [
        's_sponsor' => $nowid,
        's_main' => $message,
        's_created_time' => date('Y-m-d H:i:s', time()),
        's_title' => $title,
        's_img' => $this->file_path. date('Ymd') .'/'. $img,
        'like_num' => '0'
      ];
    }
    $result = Db::table('gjc_story')->insert($data);
    return res(1000, '添加成功', $result);
  }
  function getOneMessage()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $nowid = $nowSendM['id'];
    $StoryArr = Db::table('gjc_story')
      ->alias('s')
      ->join('user u', 'u.uid = s.s_sponsor')
      ->where('s_id', $nowid)
      ->select();
    $ConmentArr = Db::table('gjc_comment_commuity')
      ->alias('sc')
      ->join('user u', 'u.uid = sc.uid')
      ->where('s_id', $nowid)
      ->select();
    $likeArr = Db::table('gjc_story_like')
      ->alias('sl')
      ->join('user u', 'u.uid = sl.uid')
      ->where('s_id', $nowid)
      ->select();
    $result = array();
    $result = array('Story' => $StoryArr, 'Conmen' => $ConmentArr, 'Like' => $likeArr);

    return res(1000, '发送成功', $result);
  }
  function sendLikeuser()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $uesrid = $nowSendM['userid'];
    $storyId = $nowSendM['storyId'];
    $data = ['s_id' => $storyId, 'uid' => $uesrid];
    $result = Db::table('gjc_story_like')->insert($data);
    if ($result == 1) {
      return res('success', '点赞成功');
    } else {
      return res('error', '点赞失败,请稍后再试');
    }
  }
  public function sendComentUser()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $uesrid = $nowSendM['userId'];
    $main = $nowSendM['s_comment_main'];
    $sid  = $nowSendM['sid'];
    $data = ['uid' => $uesrid, 's_id' => $sid, 'sc_time' => date('Y-m-d H:i:s', time()), 's_comment_main' => $main];
    $result = Db::table('gjc_comment_commuity')->insert($data);
    return  res('success', '点赞成功', $result);
  }
  public function getusermessage()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $token = $nowSendM['token'];
    // return json($token);
    // die();
    $data = $this->redis->get($token);
    return $data;
  }
  public function sendImg()
  {
    $file = request()->file('image');

    // 移动到框架应用根目录/public/uploads/ 目录下
    if ($file) {
      $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
      if ($info) {
        // 成功上传后 获取上传信息
        $result =  $info->getFilename();
        return res('success', '保存图片成功，发表后显示', $result);
      } else {
        // 上传失败获取错误信息
        $result =  $file->getFilename();
        return res('error', '保存图片失败请稍后再试', $result);
      }
    }
  }
  public function sendIndexMsg()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $uesrid = $nowSendM['nowuerid'];
    $main = $nowSendM['nowCom'];
    $sid  = $nowSendM['nowT'];
    $data = ['uid' => $uesrid, 's_id' => $sid, 'sc_time' => date('Y-m-d H:i:s', time()), 's_comment_main' => $main];
    $result = Db::table('gjc_comment_commuity')->insert($data);
    return  res('success', '评论成功', $result);
  }
  public function getclass()
  {
    $getM = Db::table('gjc_classify')
      ->field('c_name')
      ->field('cid')
      ->select();
    $sql1 = "SELECT SUM(s.nowmeney) most FROM(SELECT price*buyer nowmeney,gid from gjc_goodsitem)s ";
    $sql2 = "SELECT MAX(most) a FROM(SELECT SUM(s.nowmeney) most FROM(SELECT price*buyer nowmeney,gid from gjc_goodsitem) s GROUP BY s.gid)f";
    $sql3 = "SELECT SUM(buyer) a from gjc_goodsitem GROUP BY gid ORDER BY a DESC";
    $aLL =  Db::query($sql1);
    $MostMoeny = Db::query($sql2);
    $MostNum = Db::query($sql3);
    $result  = [$getM, $aLL, $MostMoeny, $MostNum];
    return json($result);
  }
  public function selectIndex()
  {
    $post = getPost();
    $keyword =  $post['selName'];
    // 将字符串除了一下

    if ($post['type'] == 1) {
      $where = " g_name like '%{$keyword}%'";
    }else{
      $where = "cid = '{$post['type']}' and g_name like '%{$keyword}%'";
    }

    $res = db('goods')
    ->field('g_name , gid')
    ->where($where)
    ->select();


    return res(100,'ok',$res);





    // $nowSendM = json_decode(file_get_contents("php://input"), true);
    // $selectName = $nowSendM['selName'];
    // $selType = $nowSendM['type'];
    // if ($selType == '') {
    //   $keyword = explode(" ", $selectName);
    //   foreach ($keyword as $value) {
    //     $whereArry[] = ['like', '%' . $value . '%'];
    //   }
    //   $whereArry[] = 'or';
    //   $where['g_name'] = $whereArry;
    //   $result = Db::table('gjc_goods')
    //     ->field('g_name')
    //     ->field('gid')
    //     ->where($where)
    //     ->where("g_state",'0')
    //     ->limit(4)
    //     ->select();
    //   return json($result);
    // } else {
    //   $keyword = explode(" ", $selectName);
    //   foreach ($keyword as $value) {
    //     $whereArry[] = ['like', '%' . $value . '%'];
    //   }
    //   $whereArry[] = 'or';
    //   $where['g_name'] = $whereArry;
    //   $result = Db::table('gjc_goods')
    //     ->field('g_name')
    //     ->where($where)
    //     ->where('cid', $selType)
    //     ->limit(4)
    //     ->where("g_state",'0')
    //     ->select();
    //   return json($result);
    // }
  }
  public function getusernum()
  {
    $sql1 = "cREATE TABLE selectNum (`id` int(11) NOT NULL AUTO_INCREMENT,	`echart_time` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    $newTable = Db::execute($sql1);

    $timeArr = json_decode(file_get_contents("php://input"), true);
    foreach ($timeArr as $key => $value) {
      $data = ['echart_time' => $value];
      $result = Db::table('selectNum')->insert($data);
    }
    $sql3 = "sELECT  e.echart_time,IFNULL(s.num,0) num from
      selectNum e left join (SELECT DATE_FORMAT(create_time,'%Y-%m-%d')
      nowtime,count(*) num FROM gjc_user z GROUP 
      BY DATE_FORMAT(create_time,'%Y-%m-%d')) s on nowtime = e.echart_time";
    $selTab = Db::query($sql3);
    $sql4 = "DROP TABLE selectNum";
    Db::execute($sql4);
    $newtimeArr = [];
    $newNumArr = [];
    foreach ($selTab as $key => $value) {
      $newtimeArr[] = $value['echart_time'];
      $newNumArr[] = $value['num'];
    }
    $result = [
      $newtimeArr,
      $newNumArr
    ];
    return res(1000, '成功', $result);
  }

  public function getAddrnum()
  {
    $sql1 = "cREATE TABLE selectNum (`id` int(11) NOT NULL AUTO_INCREMENT,	`echart_time` varchar(255) NOT NULL,PRIMARY KEY (`id`)) ENGINE=MyISAM DEFAULT CHARSET=utf8;";
    Db::execute($sql1);
    $timeArr = json_decode(file_get_contents("php://input"), true);
    foreach ($timeArr as $key => $value) {
      $data = ['echart_time' => $value];
      $result = Db::table('selectNum')->insert($data);
    }
    $sql3 = "sELECT IFNULL(s.allmoney,0) price,e.echart_time from selectNum e left join(SELECT  DATE_FORMAT(o.time,'%Y-%m-%d') atime ,sum(i.price) allmoney from gjc_goodsitem 
    i,gjc_order o WHERE i.id = o.g_item_id GROUP BY DATE_FORMAT(time,'%Y-%m-%d')) s ON s.atime = e.echart_time";
    $selTab = Db::query($sql3);
    $sql4 = "DROP TABLE selectNum";
    Db::execute($sql4);
    $newtimeArr = [];
    $newNumArr = [];
    foreach ($selTab as $key => $value) {
      $newtimeArr[] = $value['echart_time'];
      $newNumArr[] = $value['price'];
    }
    $result = [
      $newtimeArr,
      $newNumArr
    ];
    return res(1000, '成功', $result);
  }


  public function getMyDymessage()
  {
    $nowSendM = json_decode(file_get_contents("php://input"), true);
    $token = $nowSendM['nowID'];
    $data = $this->redis->get($token);
    $uid = json_decode($data)->uid;
    $SelectText = Db::table('gjc_story')
      ->where('s_sponsor', $uid)
      ->select();
    if ($SelectText == []) {
      return res(1001, '暂无动态');
    } else {
      return res(1000, '获取成功', $SelectText);
    }
  }
}
